//asideFnList 当python里面的装饰器用
let asideList = []
let asideFnList = []

asideList.push('病人信息')
asideFnList.push(function () {
    showNavTitle()
    showDom(' ', '.content', table)
})

// asideList.push('信息更新')
// asideFnList.push(function () {
//     $.get('/数据更新')
//     showNavTitle()
//     showDom('微创', '.content', table)
// })

asideList.push('添加医生出院')
asideFnList.push(function (fnIndex) {
    $('.nav_title').empty().append(`<div class='back2'>${asideList[fnIndex]}</div>`)
    let rows = []
    rows.push(`<form><div>
    <div>选择管床医生</div>
    `)
    $.post('/sql/', { sql: `select name from employee ` }, function (datas) {
        let data = datas[1]
        let result = ''
        for (let i = 0; i < data.length; i++) {
            result += `<option>${data[i][0]}</option>`
        }
        result = `<select name='doctorName'>` + result
        result += `</select>`
        rows.push(result)
        rows.push(`<div>对应的病人打钩✓</div>`)
        rows.push(`<input type="submit" value="确定">`)
        rows.push(`<input type="reset" value="重置"></div>`)
        $.post('/sql/', { sql: 'select a.c_zyh as 住院号, b.管床医生, a.c_name as 姓名, c.c_sex_desc as 性别, c.c_age_str as 年龄, case when a.c_bed like "%床" then c_bed else concat(substring(a.c_bed,2,2),"床") end as 床号, c.c_ry_zd as 诊断, a.c_ry_date as 入院日期, a.c_out_date as 出院日期, a.kyye as 报销费用 from patient_zyhs a left join  assistant_doctor b on a.c_zyh = b.住院号 left join patient_details c on a.c_zyh = c.c_zyh where a.c_out_date is not null  and datediff(now(), a.c_out_date)<30 and 管床医生 is null order by 住院号' }, function (datas) {
            rows.push(checkbox_table(datas, 0))
            rows.push('</form>')
            $('.content').empty().append(rows.join(''))
            $('input[type="submit"]').on('click', function (event) {
                let title = getFormData('form')
                $.post('/post', { sql: `replace into assistant_doctor select c_zyh as 住院号, "${title['doctorName']}"  from patient_zyhs where c_zyh  in (${title['住院号']})` })
                event.preventDefault()
                document.querySelector(`.aside>div:nth-child(${fnIndex + 1})`).click()
            })
        })
    })




})

asideList.push('添加医生在院')
asideFnList.push(function (fnIndex) {
    $('.nav_title').empty().append(`<div class='back2'>${asideList[fnIndex]}</div>`)
    let rows = []
    rows.push(`<form><div>
    <div>选择管床医生</div>
    `)
    $.post('/sql/', { sql: `select name from employee ` }, function (datas) {
        let data = datas[1]
        let result = ''
        for (let i = 0; i < data.length; i++) {
            result += `<option>${data[i][0]}</option>`
        }
        result = `<select name='doctorName'>` + result
        result += `</select>`
        rows.push(result)
        rows.push(`<div>对应的病人打钩✓</div>`)
        rows.push(`<input type="submit" value="确定">`)
        rows.push(`<input type="reset" value="重置"></div>`)
        $.post('/sql/', { sql: 'select * from (select a.c_zyh as 住院号, b.管床医生, a.c_name as 姓名, c.c_sex_desc as 性别, c.c_age_str as 年龄,case when a.c_bed like "%床" then c_bed else concat(substring(a.c_bed,2,2),"床") end as 床号, c.c_ry_zd as 诊断, a.c_ry_date as 入院日期,  a.kyye as 报销费用 from patient_zyhs a left join  assistant_doctor b on a.c_zyh = b.住院号 left join patient_details c on a.c_zyh = c.c_zyh where a.c_out_date is  null and 管床医生 is null order by 床号) a union select * from (select a.c_zyh as 住院号, b.管床医生, a.c_name as 姓名, c.c_sex_desc as 性别, c.c_age_str as 年龄,case when a.c_bed like "%床" then c_bed else concat(substring(a.c_bed,2,2),"床") end as 床号, c.c_ry_zd as 诊断, a.c_ry_date as 入院日期,  a.kyye as 报销费用 from patient_zyhs a left join  assistant_doctor b on a.c_zyh = b.住院号 left join patient_details c on a.c_zyh = c.c_zyh where a.c_out_date is  null and 管床医生 is  not null order by 床号) b' }, function (datas) {
            rows.push(checkbox_table(datas, 0))
            rows.push('</form>')
            $('.content').empty().append(rows.join(''))
            $('input[type="submit"]').on('click', function (event) {
                let title = getFormData('form')
                $.post('/post', { sql: `replace into assistant_doctor select c_zyh as 住院号, "${title['doctorName']}"  from patient_zyhs where c_zyh  in (${title['住院号']})` })
                event.preventDefault()
                document.querySelector(`.aside>div:nth-child(${fnIndex + 1})`).click()
            })
        })
    })




})


asideList.push('血糖公式')
asideFnList.push(function (fnIndex) {
    $('.nav_title').empty().append(`<div class='back2'>${asideList[fnIndex]}</div>`)
    $('.content').empty().append(`<div>
    <label for="bg">血糖（mmol/L）</label>
    <input id="bg" type="text" name="bg" value="18"><br>
    <label for="bw">体重（kg）</label>
    <input id="bw" type="text" name="bw" value="50"><br>
    <label for="result">胰岛素用量（UI）</label>
    <input type="text" name="result" readonly>    
</div>
`)
    blood_glucose()

})




function showElements(datas) {
    let data = datas[1]
    let result = ''
    for (let i = 0; i < data.length; i++) {
        result += `
        <div name='${data[i][0]}'>
            <span>出${data[i][2]}</span>
            ${data[i][0]}${data[i][1]}人
            <i>入${data[i][3]}</i>
        </div>
        `
    }
    return result
}

function showNavTitle() {
    showDom(`select b.c_dept_id_desc 科室,sum(case when a.c_out_date is null then 1 else 0 end) as 人数,sum(case when datediff(a.c_out_date,now())=0  then 1 else 0 end) as 出院,sum(case when datediff(a.c_ry_date,now())=0  then 1 else 0 end) as 入院 from patient_zyhs a left join patient_details b on a.c_zyh=b.c_zyh group by 科室 having 人数>0 or 出院>0 union select '全院',sum(case when a.c_out_date is null then 1 else 0 end) as 人数,sum(case when datediff(a.c_out_date,now())=0  then 1 else 0 end) as 出院,sum(case when datediff(a.c_ry_date,now())=0  then 1 else 0 end) as 入院 from patient_zyhs a left join patient_details b on a.c_zyh=b.c_zyh`, '.nav_title', showElements)
}

$(function () {
    circle_font($('.top'), "石林仁济医院")
    showNavTitle()
    showDom('微创', '.content', table)
    // let asideList = [
    //     '病人信息',
    //     '信息更新',
    //     '获取自费',
    //     '手术统计',
    //     '费用统计',
    //     '自费统计',
    //     'MYSQL查询',
    //     '添加手术',
    //     '血糖公式'
    // ]
    for (let i = 0; i < asideList.length; i++) {
        $('.aside').append(`<div data-index='${i}'>${asideList[i]}</div>`)
    }

    $('.nav_title').on('click', 'div', function () {
        if ($(this).attr('name')) {
            showDom($(this).attr('name'), '.content', table)
            $(this).siblings().removeClass('back2')
            $(this).addClass('back2')
        }

    })


    $('.aside').on('click', 'div', function () {
        let fnIndex = $(this).data('index')
        asideFnList[fnIndex](fnIndex)
        $(this).siblings().removeClass('back2')
        $(this).addClass('back2')
    })

    $('.content').on('input', 'div>input', blood_glucose)
})

